package com.lzx.iteam.util;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import com.lzx.iteam.DialerApp;

/**
 * Created by Megan on 16/3/10.
 */
public class NetworkUtil {

    private Context mContext;

    public NetworkUtil(Context mContext) {
        this.mContext = mContext;
    }

    public static boolean checkNetwork(Context mContext) {

        ConnectivityManager manager = (ConnectivityManager)( DialerApp.mContext).getSystemService(Context.CONNECTIVITY_SERVICE);

        if (manager == null) {
            return false;
        }

        NetworkInfo networkinfo = manager.getActiveNetworkInfo();

        if (networkinfo == null || !networkinfo.isAvailable()) {
            return false;
        }

        return true;
    }

    /**
     * 判断是否有网络
     */
    private boolean checkNetworkDialog() {
        boolean flag = false;
        ConnectivityManager cwjManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cwjManager.getActiveNetworkInfo() != null)
            flag = cwjManager.getActiveNetworkInfo().isAvailable();
        if (!flag) {
            AlertDialog.Builder b = new AlertDialog.Builder(mContext).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
            b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Intent mIntent = new Intent("/");
                    ComponentName comp = new ComponentName("com.android.settings",
                            "com.android.settings.WirelessSettings");
                    mIntent.setComponent(comp);
                    mIntent.setAction("<span class=\"hilite\">android</span>.intent.action.VIEW");
                    mContext.startActivity(mIntent);
                }
            }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            }).create();
            b.show();
        }

        return flag;
    }

    /**
     * 检测是否开启wifi或gprs
     *
     * @return
     */
    public boolean checkWifiOr3G() {
        ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        //mobile 3G Data Network
        State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        //wifi
        State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();

        //如果3G网络和wifi网络都未连接，且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
        if (mobile == State.CONNECTED || mobile == State.CONNECTING)
            return true;
        if (wifi == State.CONNECTED || wifi == State.CONNECTING)
            return true;


        AlertDialog.Builder b = new android.app.AlertDialog.Builder(mContext).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
        b.setPositiveButton("设置网络", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                mContext.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));//进入无线网络配置界面
//				mContext.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); //进入手机中的wifi网络设置界面
            }
        }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        }).create();
        b.show();
        return false;

    }

    public static WifiInfo getWifiInfo(Context mContext){

        String wifiMac;
        WifiManager mWifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);

       /* if (!mWifi.isWifiEnabled()) {
            mWifi.setWifiEnabled(true);
        }*/

        WifiInfo wifiInfo = mWifi.getConnectionInfo();

        if ((wifiMac = wifiInfo.getMacAddress()) == null) {
            wifiMac = "No Wifi Device";
        }

//		查看已经连接上的WIFI信息，在Android的SDK中为我们提供了一个叫做WifiInfo的对象，这个对象可以通过WifiManager.getConnectionInfo()来获取。WifiInfo中包含了当前连接中的相关信息。
//		getBSSID()  获取BSSID属性
//		getDetailedStateOf()  获取客户端的连通性
//		getHiddenSSID()  获取SSID 是否被隐藏
//		getIpAddress()  获取IP 地址
//		getLinkSpeed()  获取连接的速度
//		getMacAddress()  获取Mac 地址
//		getRssi()  获取802.11n 网络的信号
//		getSSID()  获取SSID
//		getSupplicanState()  获取具体客户端状态的信息

        StringBuffer sb = new StringBuffer();
        sb.append("\n获取BSSID属性（所连接的WIFI设备的MAC地址）：" + wifiInfo.getBSSID());
        sb.append("\n\n获取SSID 是否被隐藏："+ wifiInfo.getHiddenSSID());
        sb.append("\n\n获取IP 地址：" + wifiInfo.getIpAddress());
        sb.append("\n\n获取连接的速度：" + wifiInfo.getLinkSpeed());
        sb.append("\n\n获取802.11n 网络的信号：" + wifiInfo.getRssi());
        sb.append("\n\n获取SSID（所连接的WIFI的网络名称）：" + wifiInfo.getSSID());
        sb.append("\n\n获取具体客户端状态的信息：" + wifiInfo.getSupplicantState());
        return wifiInfo;
    }
}
